package com.by.service.purchase.impl;

import com.by.common.bean.PageBean;
import com.by.common.bean.PageMap;
import com.by.common.crud.BaseServiceImpl;
import com.by.entity.purchase.Enquiry;
import com.by.entity.purchase.EnquiryProduct;
import com.by.mapper.common.AttachmentMapper;
import com.by.mapper.purchase.EnquiryDetailMapper;
import com.by.mapper.purchase.EnquiryMapper;
import com.by.mapper.purchase.EnquiryProductMapper;
import com.by.service.purchase.EnquiryService;
import com.by.utils.AssertsUtils;
import com.by.utils.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class EnquiryServiceImpl extends BaseServiceImpl<EnquiryMapper, Enquiry> implements EnquiryService {

    /**
     * 当前系统采询价单最大编号
     */
    private String CURRENT_MAX_CODE;

    //编码前缀
    private final String CODE_PREFIX = "XJ";

    @Autowired
    EnquiryProductMapper enquiryProductMapper;

    @Autowired
    EnquiryDetailMapper enquiryDetailMapper;

    @Autowired
    AttachmentMapper attachmentMapper;

    @Override
    public PageBean queryWaitEnquiry(Map map) {
        PageMap pageMap = new PageMap(map);
        PageHelper.startPage(pageMap.getPage(), pageMap.getPageSize());
        List list = baseMapper.queryWaitEnquriy(map);
        PageInfo pageInfo = new PageInfo(list);
        return new PageBean(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public Map queryEnquiryProduct(String id) {
        return baseMapper.queryEnquiryProduct(id);
    }


    @Transactional
    @Override
    public int saveEnquiry(Enquiry enquiry) {
        //生成询价单id
        String id = UUID.randomUUID().toString();
        enquiry.setId(id);
        //生成编码
        enquiry.setCode(getNextCode());
        //保存询价单
        int rs =baseMapper.insert(enquiry); //baseMapper.saveEnquiry(enquiry);
        if (rs == 0)
            AssertsUtils.rollback("保存询价单失败");
        //保存询价产品
        EnquiryProduct product = enquiry.getEnquiryProduct();
        //设置询价单id
        product.setEnquiryId(id);
        //保存询价产品
        rs = enquiryProductMapper.insert(product);
        if (rs == 0)
            AssertsUtils.rollback("保存询价产品失败");

        //判断明细是否有
        AssertsUtils.isNull(enquiry.getEnquiryDetails(), "询价明细不能为空");
        //保存询价明细
        rs = enquiryDetailMapper.batchSave(id, enquiry.getEnquiryDetails());
        if (rs == 0)
            AssertsUtils.rollback("保存询价明细失败");

        //判断附件是否为空
        if (null != enquiry.getAttachments() && enquiry.getAttachments().size() > 0) {
            //保存附件
            attachmentMapper.saveBatch(enquiry.getAttachments(), (byte) 2, id);
        }

        return 1;
    }

    /**
     * 获取新的编号
     *
     * @return
     */
    String getNextCode() {

        //获取当前系统日期
        String ym = DateUtils.getSystemYM();  //202210

        //1. 先判断当前内存中 是否存在最大编码
        if (null == CURRENT_MAX_CODE) {
            //2.不存在  从数据库查询  当月最大编码
            CURRENT_MAX_CODE = baseMapper.queryMaxCode();
            //计算新的编码
            if (null == CURRENT_MAX_CODE) {
                //产生新的编码  存到内存
                CURRENT_MAX_CODE = CODE_PREFIX + ym + "0001";
                return CURRENT_MAX_CODE;
            }

        }

        //获取当前编码的年月 XJ202209
        String curYm = CURRENT_MAX_CODE.substring(2, 8);
        //判断当前系统时间与现有编码curYm是否同月
        if (curYm.equals(ym)) {
            //获取后4位
            String num = CURRENT_MAX_CODE.substring(CURRENT_MAX_CODE.length() - 4);
            int newNum = Integer.parseInt(num);
            newNum++;
            CURRENT_MAX_CODE = CODE_PREFIX + ym + String.format("%04d", newNum);
        } else {
            //新的一月  编号归零
            CURRENT_MAX_CODE = CODE_PREFIX + ym + "0001";
        }
        return CURRENT_MAX_CODE;
    }
}
